package com.great.dao;

import com.great.domain.Product;
import com.great.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ProductDao {

    @Select("select * from product")
    @Results({
            @Result(column = "product_id",property = "product_id"),
            @Result(column = "product_name",property = "product_name"),
            @Result(column = "product_description",property = "product_description"),
            @Result(column = "product_price",property = "product_price"),
            @Result(column = "product_stock",property = "product_stock"),
            @Result(column = "product_fid",property = "product_fid"),
            @Result(column = "product_cid",property = "product_cid"),
            @Result(column = "product_image",property = "product_image"),
            @Result(column = "product_status",property = "product_status"),
            @Result(column = "product_fid",property = "product_fid_name",one = @One(select = "com.great.dao.CateDao.findNameById")),
            @Result(column = "product_cid",property = "product_cid_name",one = @One(select = "com.great.dao.CateDao.findNameById"))}
    )
    public List<Product> findAllProduct();//查询所有商品

    @Insert("insert into product (product_name,product_description,product_price,product_stock,product_fid,product_cid,product_image) " +
            "Values(#{product_name},#{product_description},#{product_price},#{product_stock},#{product_fid},#{product_cid},#{product_image})")
    void addproduct(Product product);//添加商品

    @Update("update product set product_stock=#{product_stock} where product_id=#{product_id} ")
    void updateproduct(@Param("product_id") Integer product_id,@Param("product_stock") int product_stock);//更新库存

    @Select("select * from product where product_id=#{product_id}")
    @Results({
            @Result(column = "product_id",property = "product_id"),
            @Result(column = "product_name",property = "product_name"),
            @Result(column = "product_description",property = "product_description"),
            @Result(column = "product_price",property = "product_price"),
            @Result(column = "product_stock",property = "product_stock"),
            @Result(column = "product_fid",property = "product_fid"),
            @Result(column = "product_cid",property = "product_cid"),
            @Result(column = "product_image",property = "product_image"),
            @Result(column = "product_status",property = "product_status"),
            @Result(column = "product_fid",property = "product_fid_name",one = @One(select = "com.great.dao.CateDao.findNameById")),
            @Result(column = "product_cid",property = "product_cid_name",one = @One(select = "com.great.dao.CateDao.findNameById"))}
    )
    public Product findproductById(Integer product_id);//查找某个商品


    @Update("update product set product_name=#{product_name},product_description=#{product_description},product_price=#{product_price}," +
            "product_stock=#{product_stock},product_fid=#{product_fid},product_cid=#{product_cid},product_image=#{product_image} where product_id=#{product_id}")
    public void productchange(Product product);//修改商品

    @Update("update product set product_status=1 where product_id=#{product_id}")
    void shangjia(Integer product_id);//上架

    @Update("update product set product_status=0 where product_id=#{product_id}")
    void xiajia(Integer product_id);//下架

    @Select({"<script>",
            "SELECT * FROM product",
            "WHERE 1=1",
            "<when test='product.product_cid!=-1'>",
            "AND  product_cid = #{product.product_cid}",
            "</when>",
            "<when test='product.product_fid!=-1'>",
            "AND product_fid = #{product.product_fid}",
            "</when>",
            "<when test='product.product_status!=-1'>",
            "AND product_status = #{product.product_status}",
            "</when>",
            "AND product_name like '%${qrysearch}%'",
            "order by product_id desc",
            "</script>"})
    @Results({
            @Result(column = "product_id",property = "product_id"),
            @Result(column = "product_name",property = "product_name"),
            @Result(column = "product_description",property = "product_description"),
            @Result(column = "product_price",property = "product_price"),
            @Result(column = "product_stock",property = "product_stock"),
            @Result(column = "product_fid",property = "product_fid"),
            @Result(column = "product_cid",property = "product_cid"),
            @Result(column = "product_image",property = "product_image"),
            @Result(column = "product_status",property = "product_status"),
            @Result(column = "product_fid",property = "product_fid_name",one = @One(select = "com.great.dao.CateDao.findNameById")),
            @Result(column = "product_cid",property = "product_cid_name",one = @One(select = "com.great.dao.CateDao.findNameById"))}
    )
    List<Product> findcheProduct(@Param("product")Product product, @Param("qrysearch")String qrysearch);//分页组合查询
}
